package pzl.com.drawrelated.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;

import pzl.com.drawrelated.R;

/**
 * @author zl.peng
 * @version [1.0, 2016-11-21]
 */
public class V_ComposeShader extends View {
    int w;
    int h;
    Paint paint;

    Bitmap biger;
    SweepGradient sweepGradient;
    BitmapShader bigerBitmapShaderCLAMP;
    LinearGradient linearGradientCLAMMultiCoclor;
    ComposeShader composeShader1;
    ComposeShader composeShader2;

    /**
     * public ComposeShader(Shader shaderA,Shader shaderB, Xfermode mode)
     * public ComposeShader(Shader shaderA,Shader shaderB, PorterDuff.Mode mode)
     *
     * @param context
     * @param attrs
     */

    public V_ComposeShader(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(1);
        biger = BitmapFactory.decodeResource(context.getResources(), R.drawable.test1);
        bigerBitmapShaderCLAMP = new BitmapShader(biger, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(1);
        w = getWidth();
        h = getHeight();
        sweepGradient = new SweepGradient(w / 2, h / 2, new int[]{Color.BLACK, Color.WHITE}, null);
        linearGradientCLAMMultiCoclor = new LinearGradient(0, 0, w, 400, new int[]{
                Color.parseColor("#f2312e"), Color.parseColor("#2ef2cb"), Color.parseColor("#d15c329f"), Color.parseColor("#e6663b")}, null, Shader.TileMode.CLAMP);
        // 混合渲染 将两个效果叠加
        composeShader1 = new ComposeShader(bigerBitmapShaderCLAMP, sweepGradient,
                PorterDuff.Mode.LIGHTEN);
        composeShader2 = new ComposeShader(bigerBitmapShaderCLAMP, linearGradientCLAMMultiCoclor,
                PorterDuff.Mode.LIGHTEN);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setShader(composeShader1);
        Rect rect = new Rect();
        rect.top = 0;
        rect.left = 0;
        rect.right = w;
        rect.bottom = 400;
        canvas.drawRect(rect, paint);

        paint.setShader(composeShader2);
        Rect rect1 = new Rect();
        rect1.top = rect.bottom;
        rect1.left = 0;
        rect1.right = w;
        rect1.bottom = rect1.top + 400;
        canvas.drawRect(rect1, paint);
    }

}
